//
//  SurePwdViewController.m
//  TengYunWaystation
//
//  Created by Johnny on 15/7/15.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "SurePwdViewController.h"
#import "SocketRequest.h"

@interface SurePwdViewController ()<UITextFieldDelegate,SocketRequestDelegate> {
    SocketRequest *_socketRequest;
}

@end

@implementation SurePwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"设置新密码";
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _inputView.layer.borderColor = LineColor.CGColor;
    _inputView.layer.borderWidth = 0.5;
    [_pwdTextField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [_pwdTextField becomeFirstResponder];
}

- (void)resetPwdSocket {
    [ToastActivity makeActivity:@"密码提交中..." InView:self.view];
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STU2"]];
    [mutableData appendBytes:[SocketMethod checkLegal:self.phone] length:16];
    [mutableData appendBytes:[SocketMethod checkLegal:self.code] length:6];
    [mutableData appendBytes:[SocketMethod checkLegal:_pwdTextField.text] length:128];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

- (void)backRootViewController {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)textFieldEditingChanged:(UITextField *)textField {
    if (textField.text.length > PwdMaxLength) {
        textField.text = [textField.text substringToIndex:PwdMaxLength];
        [textField resignFirstResponder];
        [DSToast showText:@"密码不能超过20位" InView:self.view];
    }
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    if ([code isEqualToString:SuccessSocket]) {
        [ToastActivity makeSuccess:@"密码修改成功" InView:self.view];
        [self performSelector:@selector(backRootViewController) withObject:nil afterDelay:1];
    } else {
        [ToastActivity makeError:msg InView:self.view];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [ToastActivity makeError:msg InView:self.view];
}

#pragma mark NibAction
- (IBAction)isShowPwdAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    if (button.selected) {
        button.selected = NO;
        _pwdTextField.secureTextEntry = YES;
    } else {
        button.selected = YES;
        _pwdTextField.secureTextEntry = NO;
    }
}

- (IBAction)finishResetPwdAction:(id)sender {
    [_pwdTextField resignFirstResponder];
    if (_pwdTextField.text.length <= 0) {
        [DSToast showText:@"请输入密码" InView:self.view];
    } else {
        [self resetPwdSocket];
    }
}

- (void)dealloc {
    _socketRequest = nil;
}

@end
